package com.alan.netty.barrage;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author weiBin
 * @date 2018/11/21
 */
public class WebSocketBarrageServer {
    public static void openServer(int port) {
        ServerBootstrap b = new ServerBootstrap();
        NioEventLoopGroup boss = new NioEventLoopGroup(1);
        NioEventLoopGroup worker = new NioEventLoopGroup(8);
        b.group(boss, worker).channel(NioServerSocketChannel.class).childHandler(new WebSocketBarrageChannelInitializer());
        b.option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        try {
            ChannelFuture future = b.bind(port).sync();
            System.out.println("barrage服务开启端口监听" + port);
            future.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
            System.out.println("barrage 关闭了");
        }
    }

    public static void main(String[] args) {
        WebSocketBarrageServer.openServer(8080);
    }
}
